#include "detectorparsepacket.h"
#include "detectorprotocoldata.h"
#include "common/pfcid.h"
#include "common/packetformat.h"

#include <QVariant>
#include "modulehplccmdid.h"

namespace hplc
{
namespace qinfine
{

DetectorParsePacket::DetectorParsePacket(DetectorProtocolData &data, DetectorParam &param, QObject *parent)
    : ParseCommonPacket (data, param, parent)
{

}

DetectorParsePacket::~DetectorParsePacket()
{

}

void DetectorParsePacket::parseSet(quint8 pfc, char *data)
{
    DetectorParam *param = (DetectorParam*)(m_param);
    switch (pfc)
    {
    case PFC_DETECTOR_WAVE:
    {
        param->basicParam.wave.wave = PTR2U16(data);
        emit setWave(param->basicParam.wave.wave);

        QVariant var;
        var.setValue(param->basicParam.wave.wave);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetWave, var);
    }
        break;
    case PFC_DETECTOR_WAVE2:
    {
        param->basicParam.wave2.wave = PTR2U16(data);
        emit setWave2(param->basicParam.wave2.wave);

        QVariant var;
        var.setValue(param->basicParam.wave2.wave);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetWave2, var);
    }
        break;
    case PFC_DETECTOR_TIME_CONST:
    {
        param->basicParam.timeConst = (hplc::TimeConst)(data[0]);
        emit setTimeConst(data[0]);

        QVariant var;
        var.setValue(param->basicParam.timeConst);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetTimeConst, var);
    }
        break;
    case PFC_DETECTOR_RANGE:
    {
        param->basicParam.range = (hplc::Range)(data[0]);
        emit setRange(data[0]);

        QVariant var;
        var.setValue(param->basicParam.range);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetRange, var);
    }
        break;
    case PFC_DETECTOR_LAMP_TYPE:
    {
        param->basicParam.lampType = (hplc::LampType)(data[0]);
        emit setLampType(data[0]);

        QVariant var;
        var.setValue(param->basicParam.lampType);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetLampType, var);
    }
        break;
    case PFC_DETECTOR_CHANNEL:
    {
        param->basicParam.channel = (hplc::Channel)(data[0]);
        emit setChannel(data[0]);

        QVariant var;
        var.setValue(param->basicParam.channel);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetChannel, var);
    }
        break;
    case PFC_DETECTOR_LAMP:
    {
        param->basicParam.isLampOn = data[0];
        emit setLamp(data[0]);

        QVariant var;
        var.setValue(param->basicParam.isLampOn);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetLamp, var);
    }
        break;
    case PFC_DETECTOR_ZERO:
        break;
    case PFC_DETECTOR_HOME:
    {
        param->basicParam.isHome = data[0];
        emit setHome(data[0]);

        QVariant var;
        var.setValue(param->basicParam.isHome);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetHome, var);
    }
        break;
    case PFC_DETECTOR_AU_UPLOAD_FREQ:
    {
        param->systemParam.auUploadFrequency = data[0];
        emit setAuUploadFreq(data[0]);

        QVariant var;
        var.setValue(param->systemParam.auUploadFrequency);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetAuUploadFreq, var);
    }
        break;
    case PFC_DETECTOR_AU:
    {
        quint32 au = PTR2U32(data);
        quint32 au2 = PTR2U32(data + 4);
        param->basicParam.wave.au = au;
        param->basicParam.wave2.au = au2;
        emit setAu(param->basicParam.wave.au, param->basicParam.wave2.au);

        hplc::AuPair pair;
        pair.wave = param->basicParam.wave.au;
        pair.wave2 = param->basicParam.wave2.au;
        QVariant var;
        var.setValue(pair);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetAu, var);
    }
        break;
    case PFC_DETECTOR_WAVE_REF_SAM:
    {
        param->basicParam.wave.ref = PTR2U32(data);
        param->basicParam.wave.sam = PTR2U32(data + 4);
        emit setWaveRefSam(param->basicParam.wave.ref, param->basicParam.wave.sam);

        hplc::RefSamPair pair;
        pair.ref = param->basicParam.wave.ref;
        pair.sam = param->basicParam.wave.sam;
        QVariant var;
        var.setValue(pair);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetWaveRefSam, var);
    }
        break;
    case PFC_DETECTOR_WAVE2_REF_SAM:
    {
        param->basicParam.wave.ref = PTR2U32(data);
        param->basicParam.wave.sam = PTR2U32(data + 4);
        emit setWave2RefSam(param->basicParam.wave.ref, param->basicParam.wave.sam);

        hplc::RefSamPair pair;
        pair.ref = param->basicParam.wave.ref;
        pair.sam = param->basicParam.wave.sam;
        QVariant var;
        var.setValue(pair);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetWave2RefSam, var);
    }
        break;
    case PFC_DETECTOR_SCAN_CMD:
        break;
    case PFC_DETECTOR_SCAN_AU:
    {
        quint16 wave = PTR2U16(data);
        qint32 au = PTR2U32(data + 2);
        param->basicParam.scanWaveStatus.wave = wave;
        param->basicParam.scanWaveStatus.au = au;
        emit setScanAu(wave, au);

        QVariant var;
        var.setValue(param->basicParam.scanWaveStatus);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetScanAu, var);
    }
        break;
    case PFC_DETECTOR_SCAN_PARA:
    {
        quint16 startWave = PTR2U16(data);
        quint16 stopWave = PTR2U16(data + 2);
        quint8 speed = data[4];
        emit setScanPara(startWave, stopWave, speed);

        param->basicParam.scanWaveParam.startWave = startWave;
        param->basicParam.scanWaveParam.endWave = stopWave;
        param->basicParam.scanWaveParam.speed = speed;
        QVariant var;
        var.setValue(param->basicParam.scanWaveParam);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetScanParam, var);
    }
        break;
    case PFC_DETECTOR_LAMP_USED_TIME:
    {
        quint32 time = PTR2U32(data);
        emit setLampUsedTime(time);

        param->systemParam.lampUsedTime = time;
        QVariant var;
        var.setValue(param->systemParam.lampUsedTime);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetLampUsedTime, var);
    }
        break;
    case PFC_DETECTOR_START_STOP:
    {
        param->basicParam.isStart = data[0];
        emit setStartStop(data[0]);

        QVariant var;
        var.setValue(param->basicParam.isStart);
        emit parseCompleted(hplc::qinfine::DetectorCmdID_SetStartStop, var);
    }
        break;
    case PFC_DETECTOR_EXT_SYSTEM:
    {
        DetectorProtocolData *m_d = static_cast<DetectorProtocolData*>(this->m_d);
        m_d->m_systemParser->parseSet(data[0], data + 1);
    }
        break;
    case PFC_DETECTOR_EXT_USER:
        break;
    default:
        ParseCommonPacket::parseSet(pfc, data);
        break;
    }
}

void DetectorParsePacket::parseGet(quint8 pfc, char *data)
{
    switch (pfc)
    {
    case PFC_DETECTOR_WAVE:
        break;
    case PFC_DETECTOR_WAVE2:
        break;
    case PFC_DETECTOR_TIME_CONST:
        break;
    case PFC_DETECTOR_RANGE:
        break;
    case PFC_DETECTOR_LAMP_TYPE:
        break;
    case PFC_DETECTOR_CHANNEL:
        break;
    case PFC_DETECTOR_LAMP:
        break;
    case PFC_DETECTOR_ZERO:
        break;
    case PFC_DETECTOR_HOME:
        break;
    case PFC_DETECTOR_AU_UPLOAD_FREQ:
        break;
    case PFC_DETECTOR_AU:
        break;
    case PFC_DETECTOR_WAVE_REF_SAM:
        break;
    case PFC_DETECTOR_WAVE2_REF_SAM:
        break;
    case PFC_DETECTOR_SCAN_CMD:
        break;
    case PFC_DETECTOR_SCAN_AU:
        break;
    case PFC_DETECTOR_SCAN_PARA:
        break;
    case PFC_DETECTOR_LAMP_USED_TIME:
        break;
    case PFC_DETECTOR_START_STOP:
        break;
    case PFC_DETECTOR_EXT_SYSTEM:
    {
        DetectorProtocolData *m_d = static_cast<DetectorProtocolData*>(this->m_d);
        m_d->m_systemParser->parseGet(data[0], data + 1);
    }
        break;
    case PFC_DETECTOR_EXT_USER:
        break;
    default:
        ParseCommonPacket::parseGet(pfc, data);
        break;
    }
}

} // qinfine
} // hplc
